package com.winning.pmph.ctrl;

import com.winning.pmph.entity.IncreaseBookQrdiy;
import com.winning.pmph.entity.IncreaseQrDiy;
import com.winning.pmph.service.IncreaseBookQrdiyService;
import com.winning.pmph.service.IncreaseQrDiyService;
import com.winning.pmph.utils.ApplicationContextUtil;
import com.winning.pmph.utils.UploadFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "图书增值服务二维码样式自定义")
@RestController
@RequestMapping("/pmph/action/increaseQrDiy")
public class IncreaseQrDiyCtrl {

    @Autowired
    IncreaseQrDiyService increaseQrDiyService;

    @Autowired
    IncreaseBookQrdiyService increaseBookQrdiyService;

    @ApiOperation(value = "selectList", notes = "二维码自定义素材列表")
    @GetMapping("selectList")
    public List<IncreaseQrDiy> selectList() {
        return increaseQrDiyService.lambdaQuery().list();
    }

    @ApiOperation(value = "selectBookQrDiy", notes = "增值图书二维码自定义素材列表")
    @GetMapping("selectBookQrDiy")
    public List<IncreaseBookQrdiy> selectBookQrDiy(String bookId) {
        return this.increaseBookQrdiyService
                .lambdaQuery()
                .eq(IncreaseBookQrdiy::getBookId, bookId)
                .list();
    }

    @ApiOperation(value = "uploadFile", notes = "增值图书自定义二维码上传")
    @RequestMapping("uploadFile")
    public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String, Object> result = new HashMap<>();
        String path = "qrlogo/";
        String savePath = ApplicationContextUtil.BASE_DOWN_FORDER + path;
        String name = UploadFileUtil.uploadFile(file, savePath, true);
        result.put("path", "/"+ path +name);
        result.put("originalName", file.getOriginalFilename());
        return result;
    }

    /**
     * 保存图书二维码自定义中间logo
     *
     * @param increaseBookQrdiy
     * @return
     */
    @ApiOperation(value = "saveBookQrDiy", notes = "增值图书自定义二维码保存")
    @PostMapping("saveBookQrDiy")
    public void saveBookQrDiy(@RequestBody IncreaseBookQrdiy increaseBookQrdiy) {
        this.increaseBookQrdiyService.saveOrUpdate(increaseBookQrdiy);
    }

    /**
     * 重置图书二维码中间logo自定义
     *
     * @param bookQrDiyId
     * @return
     */
    @ApiOperation(value = "deleteBookQrDiy", notes = "增值图书自定义二维码重置")
    @RequestMapping("deleteBookQrDiy")
    public void deleteBookQrDiy(String bookQrDiyId) {
        this.increaseBookQrdiyService.removeById(bookQrDiyId);
    }

    /**
     * 获取图书自定义的二维码logo
     *
     * @param bookId
     * @return
     */
    @ApiOperation(value = "getQrDiyByBook", notes = "增值图书二维码查询")
    @PostMapping("getQrDiyByBook")
    public List<IncreaseBookQrdiy> getQrDiyByBook(String bookId) {
        return this.increaseBookQrdiyService.lambdaQuery().eq(IncreaseBookQrdiy::getBookId, bookId).list();
    }

    public static void main(String[] args) {
        System.out.println(System.getProperty("user.home").replace("\\", "/") + "/download/");
    }
}
